package com.h3c.gm.controller

import com.google.common.collect.Lists
import com.h3c.gm.common.Response
import com.h3c.gm.common.utils.ControllerUtils
import com.h3c.gm.entity.DO.BusHostSpecDO
import com.h3c.gm.entity.VO.BusHostSpecVO
import com.h3c.gm.service.BusHostSpecService
import com.sun.org.apache.regexp.internal.RE
import org.joda.time.DateTime
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.Mapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

import javax.annotation.Resource

/**
 * Created by d14287 on 2017/6/26.
 */
@RestController
class BusHostSpecController {

    static Logger logger = LoggerFactory.getLogger(BusHostSpecController.class)
    @Resource
    BusHostSpecService busHostSpecService
    /**
     * 统计虚拟机信息
     * @return
     */
    @RequestMapping(value = 'host/statistics', method = RequestMethod.GET)
    def getHostsStatistics() {
        def response = new Response()
        response.ofSuccess(busHostSpecService.getHostsStatistics())
    }
    /**
     * 云主机状态统计，正常/异常/关闭/回收站
     */
    @RequestMapping(value = 'host/status/statistics/{orgkey}', method = RequestMethod.GET)
    def getHostsStatusStatistics(@PathVariable(name = 'orgkey') String orgkey) {
        Response.instance().ofSuccess(busHostSpecService.getHostsStatusStatistics(orgkey))
    }

    /**
     * 统计省份虚拟机信息
     * @param province
     * @return
     */
    @RequestMapping(value = 'host/prostatistics/{province}', method = RequestMethod.GET)
    def getProHostsStatistics(@PathVariable(name = 'province') String province) {
        def response = new Response()
        response.ofSuccess(busHostSpecService.getProHostsStatistics(province))
    }

    /**
     *
     * 按月统计主机/cpu/内存12个月新增
     * @return
     */
    @RequestMapping(value = 'host/monthstatistics', method = RequestMethod.GET)
    def getMonthStatistics() {
        def response = new Response()
        def to = DateTime.now().dayOfMonth().withMaximumValue()
        def from = to.minusMonths(11).dayOfMonth().withMinimumValue()
        def contentList = Lists.newArrayList()
        contentList.add('host')
        contentList.add('cpu')
        contentList.add('ram')
        def initDate = ControllerUtils.initMonthStatistics(from, to, contentList)
        List<Map<String, Object>> dataList = busHostSpecService.getMonthStatistics(from.toDate(), to.toDate())
        dataList.each {
            item ->
                String dateStr = String.valueOf(item.get('m'))
                for (int i = 0; i <= initDate.size(); i++) {
                    String initDateStr = String.valueOf(initDate.get(i).get('m'))
                    if (dateStr.equals(initDateStr)) {
                        initDate.set(i, item)
                        break
                    }
                }
        }
        response.ofSuccess(initDate)

    }
    /**
     *
     * 按省份按月统计主机/cpu/内存12个月新增
     * @return
     */
    @RequestMapping(value = 'host/promonthstatistics/{province}', method = RequestMethod.GET)
    def getProMonthStatistics(@PathVariable(name = 'province') String province) {
        def response = new Response()
        def to = DateTime.now().dayOfMonth().withMaximumValue()
        def from = to.minusMonths(11).dayOfMonth().withMinimumValue()
        def contentList = Lists.newArrayList()
        contentList.add('host')
        contentList.add('cpu')
        contentList.add('ram')
        def initDate = ControllerUtils.initMonthStatistics(from, to, contentList)
        List<Map<String, Object>> dataList = busHostSpecService.getProMonthStatistics(from.toDate(), to.toDate(), province)
        dataList.each {
            item ->
                String dateStr = String.valueOf(item.get('m'))
                for (int i = 0; i <= initDate.size(); i++) {
                    String initDateStr = String.valueOf(initDate.get(i).get('m'))
                    if (dateStr.equals(initDateStr)) {
                        initDate.set(i, item)
                        break
                    }
                }
        }
        response.ofSuccess(initDate)
    }
}